/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.WechatMenuDao;
import com.boxin.ims.modules.wechat.entity.WechatMenu;
import com.boxin.ims.modules.wechat.utils.WechatMenuUtils;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 微信自定义菜单Service
 * @author Jakemanse
 * @version 2013-09-11
 */
@Component
@Transactional(readOnly = true)
public class WechatMenuService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(WechatMenuService.class);
	
	@Autowired
	private WechatMenuDao wechatMenuDao;
	
	public WechatMenu get(Long id) {
		return wechatMenuDao.findOne(id);
	}
	
	/*
	 * 根据事件值得到对象
	 */
	public WechatMenu getByKey(String key) {
		DetachedCriteria dc = wechatMenuDao.createDetachedCriteria();
		dc.add(Restrictions.eq("eventKey", key));
		List<WechatMenu>  menus = wechatMenuDao.find(dc);
		return (menus!=null && menus.size()>0) ? menus.get(0): null;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-12  上午1:04:25
	 * @function <p> 根据UserId 查询 该微的主菜单 </p>
	 * @param userId
	 * @return
	 */
	public List<WechatMenu> findMainMenusByUserId(Long userId){
		DetachedCriteria dc = wechatMenuDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("parent.id", 0L));
		List<WechatMenu>  menus = wechatMenuDao.find(dc);
		return menus;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-12  上午1:06:02
	 * @function <p> 查询子菜单  </p>
	 * @param parentId
	 * @return
	 */
	public List<WechatMenu> findChildrenByParentId(Long parentId){
		DetachedCriteria dc = wechatMenuDao.createDetachedCriteria();
		dc.add(Restrictions.eq("parent.id", parentId));
		List<WechatMenu>  menus = wechatMenuDao.find(dc);
		return menus;
	}
	
	public List<WechatMenu> findAll( WechatMenu wechatMenu) {
		DetachedCriteria dc = wechatMenuDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", wechatMenu.getUser().getId()));
		dc.addOrder(Order.desc("sort"));
		//dc.add(Restrictions.ne("parent.id", -1L));
		return wechatMenuDao.find( dc);
	}
	
	public List<WechatMenu> findAllByUserId( Long userId) {
		DetachedCriteria dc = wechatMenuDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.addOrder(Order.desc("sort"));
		//dc.add(Restrictions.ne("parent.id", -1L));
		return wechatMenuDao.find( dc);
	}
	
	
	public Page<WechatMenu> find(Page<WechatMenu> page, WechatMenu wechatMenu) {
		DetachedCriteria dc = wechatMenuDao.createDetachedCriteria();
		if (wechatMenu.getUser()!=null && wechatMenu.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", wechatMenu.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(wechatMenu.getName())){
			dc.add(Restrictions.like("name", "%"+wechatMenu.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(wechatMenu.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+wechatMenu.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", WechatMenu.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("sort"));
		return wechatMenuDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(WechatMenu wechatMenu) {
		 
		
		wechatMenuDao.save(wechatMenu);
		
		if(wechatMenu.getId()==null || com.thinkgem.jeesite.common.utils.StringUtils.isEmpty(wechatMenu.getEventKey())){
			wechatMenu.setEventKey(WechatMenuUtils.createMenuKey(wechatMenu));
			if(wechatMenu.getParent() == null || wechatMenu.getParent().getId() == 0L){	//一级菜单
				wechatMenu.setLevel(WechatMenu.LEVEL_PARENT);
			}else{
				wechatMenu.setLevel(WechatMenu.LEVEL_CHILD);	//子菜单
			}
			wechatMenuDao.save(wechatMenu);
		}
		
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		wechatMenuDao.deleteById(id);
	}
	
}
